package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ActivityList;
import com.mofang.powerdekorhelper.model.PastActivityList;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.ActivityListView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/24 0024.
 */

public class ActiveListPersenter extends BasePresent<ActivityListView> {

    RetrofitSerives retrofitSerives;
    Call<ActivityList> activityListCall;
    Call<PastActivityList> pastActivityListCall;
    Call<ResultMessage4> node3ImageCall;

    /**
     * 参数待定
     *
     * @param page
     * @param pageSize
     */

    public void getActivityList(int page, int pageSize, Integer userId, String searchCode) {

        view.showProgress();

        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("page", page);
            jsonParams.put("pageSize", pageSize);
            JSONObject param = new JSONObject();
            param.put("user_id", userId);
            param.put("activity_code", searchCode);
            jsonParams.put("param", param);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        String requestStr = jsonParams.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);

        activityListCall = retrofitSerives.getCurrentActivityList(body);

        activityListCall.enqueue(new Callback<ActivityList>() {
            @Override
            public void onResponse(Call<ActivityList> call, Response<ActivityList> response) {
                if (response.isSuccessful()) {

                    if (response.body().getResult() != null) {

                        if (response.body().getResult().getData() != null) {

                            if (response.body().getResult().getData().size() > 0) {
                                view.setActivityList(response.body());
                            } else {
                                view.showNullLayout();
                            }
                        } else {

                        }
                    } else {

                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ActivityList> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }


    public void getPastActivityList(int page, int pageSize, Integer shop_id) {

        view.showProgress();

        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("page", page);
            jsonParams.put("pageSize", pageSize);
            JSONObject param = new JSONObject();
            param.put("shop_id", shop_id);
            jsonParams.put("param", param);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        String requestStr = jsonParams.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);

        pastActivityListCall = retrofitSerives.getPastActivityList(body);

        pastActivityListCall.enqueue(new Callback<PastActivityList>() {
            @Override
            public void onResponse(Call<PastActivityList> call, Response<PastActivityList> response) {

                if (response.isSuccessful()) {

                    if (response.body() != null) {
                        if (response.body().getData().size() > 0) {
                            view.setPastActivityList(response.body());
                        } else {
                            view.showNullLayout();
                        }
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<PastActivityList> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    public void getNode3Image(int node3Id) {
        view.showProgress();

        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("node3", node3Id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String requestStr = jsonParams.toString();
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);

        node3ImageCall = retrofitSerives.getNode3Image(body);
        node3ImageCall.enqueue(new Callback<ResultMessage4>() {
            @Override
            public void onResponse(Call<ResultMessage4> call, Response<ResultMessage4> response) {
                if (response.isSuccessful()) {
                    view.setNode3Image(response.body());
                } else {
                    view.hideProgress();
                }
            }

            @Override
            public void onFailure(Call<ResultMessage4> call, Throwable t) {
                view.hideProgress();
            }
        });
    }

    @Override
    public void stopRequest() {

    }
}
